package com.ruoyi.im.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * 消息服务
 */
@Service
public class MsgService {

    private final String MSG_MAX_ID = "ws:session:idmax:";

    private final String USER_INFO_MAX_ID = "user:userinfo:idmax:";

    private final String MSG_GROUP_MAX_ID = "ws:session:group:idmax:";

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public Integer geneMsgId(Integer receiverId, Integer senderId) {
        ValueOperations<String, Object> op = redisTemplate.opsForValue();
        String key = receiverId + "_" + senderId;
        Long incrKey = op.increment(MSG_MAX_ID + ":" + key);
        String key1 = senderId + "_" + receiverId;
        Long incrKey1 = op.increment(MSG_MAX_ID + ":" + key1);
        return Math.max(incrKey.intValue(), incrKey1.intValue());
    }

    public Integer geneMsgGroupId(Integer groupId) {
        ValueOperations<String, Object> op = redisTemplate.opsForValue();
        String key = MSG_GROUP_MAX_ID + groupId;
        Long incrKey = op.increment(key);
        return incrKey.intValue();
    }

    public Integer geneUserId() {
        ValueOperations<String, Object> op = redisTemplate.opsForValue();
        Long incrKey = op.increment(USER_INFO_MAX_ID);
        return incrKey.intValue();
    }


}
